<?php

	class zhg extends admincore{

		//管理员信息表
		private $table_admin = 'syt_admin';
		//管理员所在权限分组
		private $table_record = 'syt_checkin';
		//项目
		private $table_project='syt_project';	

	
	    /**
		* 根据项目ID，得到项目的信息
		* @param int $project_id 项目的编号ID
		* @param string $cols    项目字段内容 
		*/
		public function info($project_id,$cols='*'){
			if(!is_numeric($project_id))return false;
			
			$sql = "SELECT {$cols} FROM {$this->table_project} WHERE id={$project_id}";
			return $this->_mysql(0)->findone($sql);
		}


        /**
         * 查看项目最新的记录信息
         * @param int $pid  项目id
         * @param string $data
         */
		public function total($pid,$data=false){
			if(!is_numeric($pid))return false;
			//1-挖机 2-计时   3-渣车
			$sqlt="SELECT count(id) as num,did FROM {$this->table_record} Where type<>2 AND coord_x>0 AND pid={$pid} ";
			$sql="SELECT max(id) as id FROM {$this->table_record} Where type<>2 AND pid={$pid} ";
				
            if($data['time']){
            	$time_end = strtotime($data['time']);
                $time_start = strtotime(date("Y-m-d",$time_end));
                $sql .= " AND ctime>=".$time_start." AND ctime<=".$time_end;
                $sqlt.= " AND ctime>=".$time_start." AND ctime<=".$time_end;
            }else{
            	$time_end = time();
            	$time_start = strtotime(date("Y-m-d",$time_end));
            	$sql .= " AND ctime>=".$time_start." AND ctime<=".$time_end;            	
            	$sqlt.= " AND ctime>=".$time_start." AND ctime<=".$time_end;
            }
			
			$sql.=" GROUP BY did";
			$sqlt.=" GROUP BY did";
			$result=$this->_mysql(0)->find($sql);			
			if(empty($result))return false;
			$res=$this->_mysql(0)->find($sqlt);
			
			foreach($result as $vo)$ids[]=$vo['id'];										
			$ids=implode(',',$ids);
									
			$sql="SELECT did,pid,coord_x,coord_y,ctime,type,doper,dophone,dname FROM {$this->table_record} WHERE id IN (".$ids.") order by dname asc";			
			$list=$this->_mysql(0)->find($sql);
			if(empty($list))return false;
			
			foreach($res as $vo)$ary[$vo['did']]=$vo['num'];
			
			foreach($list as $key=>$vo){
				$list[$key]['ctime']=date("Y-m-d H:i",$vo['ctime']);
				$num=$ary[$vo['did']];
				$list[$key]['num']=$num?$num:1;
				$cmplist[$vo['type']][]=$list[$key];
			}			

			$rtn['list']=$list;
			$rtn['list_type']=$cmplist;
			
			return $rtn;
		}

	

}